# -*- coding: utf-8 -*-
"""
Created on Thu Oct 18 19:36:34 2018

@author: HP
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from my_data_describe import my_data_describe
import scipy.stats as st

# 设定打印dataframe显示的列数量
pd.set_option('display.max_columns',20)
pd.set_option('display.max_rows',100)
#pd.set_option('display.height',100)
pd.set_option('display.width',65)


train_raw = pd.read_csv('../input/train.csv',index_col=0)
y = train_raw[['SalePrice']]

df_describe = my_data_describe(train_raw)
df_describe

df_describe[df_describe.na_rate>=0.20].index

# 数字类型特征
numeric_features = train_raw.select_dtypes(include=[np.number])

# 偏度（正态分布=0）和峰度（正态分布=3）
train_raw.skew(), train_raw.kurt()

#%%画图
fig = plt.figure()
plt.bar(train_raw.MSSubClass.value_counts().index, train_raw.MSSubClass.value_counts())
# 热力图
sns.heatmap(data=train_raw.corr())

# 直方图
plt.hist(train_raw.kurt(), color ='blue')
#plt.hist(train_raw.SalePrice, color ='blue', bins=100)
sns.distplot(train_raw['SalePrice']);
sns.distplot(np.log(train_raw.SalePrice))

# 折线图
meansale = train_raw.groupby('YearBuilt').mean().SalePrice
plt.plot(meansale.index,meansale)
# 柱状图
train_raw.FireplaceQu.value_counts().plot(kind='bar')
train_raw.groupby('FireplaceQu').median()['SalePrice'].plot(kind = 'bar',color = 'blue')
# 箱型图
from my_boxplot import my_boxplot
my_boxplot(train_raw.FireplaceQu, train_raw.SalePrice)

#sns.distplot(train_x.MSSubClass, kde=False, fit=st.johnsonsu)
#sns.distplot(train_x.MSSubClass)
# 频率分布
sns.distplot(y, kde=False, fit=st.lognorm)
sns.distplot(np.log(y), fit=st.norm)
plt.hist(y.SalePrice,bins=50)

sns.distplot(train_raw.kurt(),color='r',axlabel ='Kurtosis',norm_hist= False, kde = True,rug = False)
plt.hist(train_raw.kurt())
#多变量图
columns = ['SalePrice','OverallQual','TotalBsmtSF','GrLivArea']
sns.pairplot(train_raw[columns],size = 2 ,kind ='scatter',diag_kind='kde')

var = 'OverallQual'
data = pd.concat([train_raw['SalePrice'], train_raw[var]], axis=1)
data.plot.scatter(x=var, y='SalePrice', ylim=(0,800000))
# 相关性
correlation = numeric_features.corr()
correlation['SalePrice'].sort_values(ascending = False)



has_rank = [col for col in train_raw if 'TA' in list(train_raw[col])]
train_raw[['ExterQual',
 'ExterCond',
 'BsmtQual',
 'BsmtCond',
 'HeatingQC',
 'KitchenQual',
 'FireplaceQu',
 'GarageQual',
 'GarageCond']]

train_raw_log = train_raw.copy()
train_raw_log['SalePrice'] = np.log(train_raw['SalePrice'])

series_temp = train_raw_log[['ExterQual','SalePrice']].groupby('ExterQual').mean()
series_temp.to_dict()

sns.barplot(data=train_raw,x='ExterCond',y='SalePrice', estimator=np.mean)
